<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Traits\HasDateTimeFormatter;

class StoreOrder extends Model
{
    use HasDateTimeFormatter;
    
    protected $table = 'ims_yz_plugin_store_order';
    protected $guarded = [''];
    
    // 禁用Laravel的自动时间戳管理
    public $timestamps = false;
    
    const PAGE_SIZE = 10;
    const INCOME_TYPE_NAME = '门店提现';
    const HAS_WITHDRAW = 1;

    /**
     * 获取订单列表
     */
    public static function getOrderList($params)
    {
        return self::builder()->search($params);
    }
    
    /**
     * 根据订单ID获取订单
     */
    public static function getOrderByOrderId($order_id)
    {
        return self::builder()->byOrderId($order_id);
    }
    
    /**
     * 构建查询
     */
    public static function builder()
    {
        return self::with([
            'store',
            'order'
        ]);
    }
    
    /**
     * 按订单ID筛选
     */
    public function scopeByOrderId($query, $order_id)
    {
        return $query->where('order_id', $order_id);
    }

    /**
     * 搜索范围
     */
    public function scopeSearch($query, $params)
    {
        if (!empty($params['store'])) {
            $store_params = $params['store'];
            
            // 核销员ID搜索
            if (!empty($store_params['clerk_id'])) {
                $query->where('verification_clerk_id', $store_params['clerk_id']);
            }
            
            $query->whereHas('store', function ($store) use ($store_params) {
                // 门店id搜索
                if (!empty($store_params['store_id'])) {
                    $store->where('id', $store_params['store_id']);
                }
                
                // 门店省市区搜索
                if (!empty($store_params['province_id'])) {
                    $store->where('province_id', $store_params['province_id']);
                }
                if (!empty($store_params['city_id'])) {
                    $store->where('city_id', $store_params['city_id']);
                }
                if (!empty($store_params['district_id'])) {
                    $store->where('district_id', $store_params['district_id']);
                }
                if (!empty($store_params['street_id'])) {
                    $store->where('street_id', $store_params['street_id']);
                }
                
                // 门店名称搜索
                if (!empty($store_params['store_name'])) {
                    $store->where('store_name', 'like', '%' . $store_params['store_name'] . '%');
                }
                
                // 门店分类搜索
                if (!empty($store_params['category'])) {
                    $store->where('category_id', $store_params['category']);
                }
                
                // 门店微信搜索
                if (!empty($store_params['member'])) {
                    $store->whereHas('member', function ($member) use ($store_params) {
                        $member->select('uid', 'nickname', 'realname', 'mobile', 'avatar')
                            ->where('realname', 'like', '%' . $store_params['member'] . '%')
                            ->orWhere('mobile', 'like', '%' . $store_params['member'] . '%')
                            ->orWhere('nickname', 'like', '%' . $store_params['member'] . '%');
                    });
                }
            });
        }
        
        if (!empty($params['order'])) {
            $query->whereHas('order', function ($order) use ($params) {
                if (!empty($params['order']['order_sn'])) {
                    $order->where('order_sn', 'like', "%{$params['order']['order_sn']}%");
                }
                
                if (!empty($params['order']['member'])) {
                    $order->whereHas('user', function ($member) use ($params) {
                        $member->select('id', 'nickname', 'realname', 'mobile', 'avatar')
                            ->where('realname', 'like', '%' . $params['order']['member'] . '%')
                            ->orWhere('mobile', 'like', '%' . $params['order']['member'] . '%')
                            ->orWhere('nickname', 'like', '%' . $params['order']['member'] . '%');
                    });
                }
                
                // 支付方式
                if (!empty($params['order']['pay_type'])) {
                    $order->where('pay_type_id', $params['order']['pay_type'])->where('status', '>', '0');
                }
                
                // 订单状态
                if (isset($params['order']['status']) && $params['order']['status'] !== '') {
                    $order->where('status', $params['order']['status']);
                }
                
                // 操作时间范围
                if (!empty($params['order']['field']) && !empty($params['time_range'])) {
                    $range = [strtotime($params['time_range']['start']), strtotime($params['time_range']['end'])];
                    $order->whereBetween($params['order']['field'], $range);
                }
            });
        }
        
        return $query;
    }
    
    /**
     * 关联门店
     */
    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id');
    }
    
    /**
     * 关联订单
     */
    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id');
    }
    
    /**
     * 关联核销员
     */
    public function clerkMember()
    {
        return $this->belongsTo(User::class, 'verification_clerk_id', 'id');
    }
}
